package cn.kgc.crowdfundingadmin2.controller;

import cn.kgc.crowdfundingadmin2.common.ResultEntity;
import cn.kgc.crowdfundingadmin2.pojo.Role;
import cn.kgc.crowdfundingadmin2.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class RoleController {

    @Resource
    RoleService roleService;

    @RequestMapping("/role/search/by/keyword")
    @ResponseBody
    public ResultEntity<PageInfo<Role>>  searchByKeyword(@RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
                                                         @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
                                                         @RequestParam(value="keyword", defaultValue="",required = false) String keyword){
        //调用Service获取数据
        PageInfo<Role> pageInfo = roleService.queryForKeywordWithPage(pageNum, pageSize, keyword);
        return ResultEntity.successWithoutData(pageInfo);//{result:SUCCESS,message:NO_MESSAGE,data:{total:100,pages:5,list:[{},{}]}}
    }

    @ResponseBody
    @RequestMapping("/role/get/list/by/id/list")
    public ResultEntity<List<Role>> getListByIDList(@RequestBody List<Integer> roleIdList){
        List<Role> roleListByIdList = roleService.getRoleListByIdList(roleIdList);
        return ResultEntity.successWithoutData(roleListByIdList);
    }

    @ResponseBody
    @RequestMapping("/role/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> roleIdList){

        //调用Service删除
        roleService.batchRemove(roleIdList);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/role/save/role")
    public ResultEntity<String> saveRole(String roleName){
        //调用service保存
        roleService.saveRole(roleName);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/role/update/role")
    public ResultEntity<String> updateRole(Role role){
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }
}
